package com.aplink.generic.handler;

import android.os.Handler;
import android.os.Message;

import com.aplink.generic.GenericApplication;
import com.aplink.generic.GenericFragment;
import com.aplink.generic.cache.MasterConfigManager;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.DialogStatus;
import com.aplink.generic.fragments.LanguageDialogFragment;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.sharepreference.GenericSharePreference;

public class EventHandler extends Handler {
	private final GenericFragment mGenericFragment;

	public EventHandler(final GenericFragment genericFragment) {
		this.mGenericFragment = genericFragment;
	}

	private void handleConfirmDialogGenericHandler(
	        final DialogStatus alertDialogStatus, final ActionType actionType,
	        final Message message) {
		this.mGenericFragment.getResponseHandler().sendAction(actionType);
	}

	@Override
	public void handleMessage(final Message msg) {
		final ActionType actionType = ActionType.get(msg.what);
		if (mGenericFragment.hasRegisterProgressDialog(actionType)) {
			mGenericFragment.getProgressDialog(actionType).show();
		}
		if (actionType.equals(ActionType.LOGIN)) {
			login(actionType);
		} else if (actionType.equals(ActionType.LOGIN_OFFLINE)) {
			this.mGenericFragment.getDatabaseHandler().sendAction(
			        ActionType.LOGIN_OFFLINE);
		} else if (actionType.equals(ActionType.LOGOUT)) {
			handleConfirmDialogGenericHandler(DialogStatus.CONFIRM,
			        ActionType.LOGOUT, msg);
		} else if (actionType.equals(ActionType.RESET_APPLICATION)) {
			handleConfirmDialogGenericHandler(DialogStatus.CONFIRM,
			        ActionType.RESET_APPLICATION, msg);
		} else if (actionType.equals(ActionType.DOWNLOAD)) {
			if (GenericApplication.getInstance().isNetworkAvailable()) {
				handlerOfflineMode(ActionType.DOWNLOAD, msg);
			} else {
				this.mGenericFragment.getAlertDialog().show(actionType,
				        DialogStatus.NO_INTERNET_ACCESS);
			}
		} else if (actionType.equals(ActionType.DOWNLOAD_ALL)) {
			if (GenericApplication.getInstance().isNetworkAvailable()) {
				this.mGenericFragment.getWebserviceHandler().sendAction(
				        actionType);
			} else {
				this.mGenericFragment.getAlertDialog().show(actionType,
				        DialogStatus.NO_INTERNET_ACCESS);
			}
		} else if (actionType.equals(ActionType.UPLOAD)) {
			if (GenericApplication.getInstance().isNetworkAvailable()) {
				handlerOfflineMode(ActionType.UPLOAD, msg);
			} else {
				this.mGenericFragment.getAlertDialog().show(actionType,
				        DialogStatus.NO_INTERNET_ACCESS);
			}
		} else if (actionType.equals(ActionType.NEXT_PAGE)) {
			this.mGenericFragment.getResponseHandler().sendAction(
			        ActionType.NEXT_PAGE);
		} else if (actionType.equals(ActionType.INSERT)) {
			handlerOfflineMode(actionType, msg);
		} else if (actionType.equals(ActionType.DELETE)) {
			handlerOfflineMode(actionType, msg);
		} else if (actionType.equals(ActionType.UPDATE)) {
			handlerOfflineMode(actionType, msg);
		} else if (actionType.equals(ActionType.SAVE)) {
			handlerOfflineMode(actionType, msg);
		} else if (actionType.equals(ActionType.REFRESH_PAGE)) {
			this.mGenericFragment.getResponseHandler().sendAction(
			        ActionType.REFRESH_PAGE);
		} else if (actionType.equals(ActionType.CLOSE)) {
			this.mGenericFragment.getResponseHandler().sendAction(
			        ActionType.CLOSE);
		} else if (actionType.equals(ActionType.FORGOT_PASSWORD)) {
			this.mGenericFragment.getWebserviceHandler().sendAction(
			        ActionType.FORGOT_PASSWORD);
		} else if (actionType.equals(ActionType.CLEAR_FORM)) {
			this.mGenericFragment.clearAll();
		} else if (actionType.equals(ActionType.CHANGE_LANGUAGE)) {
			LanguageDialogFragment.newInstance(mGenericFragment).show(
			        mGenericFragment.getFragmentManager(), "LANGUAGE");
		}
	}

	private void handlerOfflineMode(final ActionType actionType,
	        final Message message) {
		if (GenericSharePreference.getInstance().isOfflineMode()
		        && !actionType.equals(ActionType.UPLOAD)
		        && !actionType.equals(ActionType.DOWNLOAD)
		        && !actionType.equals(ActionType.DOWNLOAD_ALL)) {
			this.mGenericFragment.getDatabaseHandler().sendAction(actionType);
		} else {
			if (GenericApplication.getInstance().isNetworkAvailable()) {
				this.mGenericFragment.getWebserviceHandler().sendAction(
				        actionType);
			} else {
				this.mGenericFragment.getAlertDialog().show(actionType,
				        DialogStatus.NO_INTERNET_ACCESS);
			}
		}
	}

	private void login(final ActionType actionType) {
		final MasterConfig masterConfig = MasterConfigManager.getInstance()
		        .get(GenericSharePreference.getInstance()
		                .getMasterConfigFileID());
		if (masterConfig.isDemo()) {
			if (masterConfig.isHasOfflineMode()) {
				sendEmptyMessage(ActionType.LOGIN_OFFLINE.getCode());
			}
		} else {
			if ((GenericSharePreference.getInstance().isFirstStart())
			        || (GenericSharePreference.getInstance().isInvalidToken())) {
				if (GenericApplication.getInstance().isNetworkAvailable()) {
					this.mGenericFragment.getWebserviceHandler().sendAction(
					        ActionType.LOGIN);
				} else {
					this.mGenericFragment.getProgressDialog(ActionType.LOGIN)
					        .dismiss();
					this.mGenericFragment.getAlertDialog().show(actionType,
					        DialogStatus.NO_INTERNET_ACCESS);
				}
			} else {
				sendEmptyMessage(ActionType.LOGIN_OFFLINE.getCode());
			}
		}
	}

	public void sendAction(final ActionType actionType) {
		sendEmptyMessage(actionType.getCode());
	}

	public void sendMessage(final ActionType actionType, final Object object) {
		final Message message = Message.obtain(this, actionType.getCode(),
		        object);
		message.sendToTarget();
	}
}
